package com.library.ui;

import com.library.model.Reader;
import com.library.service.ReaderService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class ReaderManagementPanel extends JPanel {
    private JTable readerTable;
    private DefaultTableModel tableModel;
    private ReaderService readerService = new ReaderService();

    public ReaderManagementPanel() {
        setLayout(new BorderLayout());

        String[] columnNames = {"读者编号", "姓名", "部门", "性别", "电话"};
        tableModel = new DefaultTableModel(columnNames, 0);
        readerTable = new JTable(tableModel);
        add(new JScrollPane(readerTable), BorderLayout.CENTER);

        JButton addBtn = new JButton("新增读者");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addReaderDialog();
            }
        });
        JButton updateBtn = new JButton("修改选中读者");
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateSelectedReader();
            }
        });
        JButton deleteBtn = new JButton("删除选中读者");
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteSelectedReader();
            }
        });
        JPanel btnPanel = new JPanel();
        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        add(btnPanel, BorderLayout.SOUTH);

        List<Reader> readers = readerService.getAllReaders();
        updateTable(readers);
    }

    private void updateTable(List<Reader> readers) {
        tableModel.setRowCount(0);
        for (Reader reader : readers) {
            tableModel.addRow(new Object[]{
                    reader.getReaderNumber(),
                    reader.getName(),
                    reader.getDepartment(),
                    reader.getGender(),
                    reader.getTelephone()
            });
        }
    }

    private void addReaderDialog() {
        JTextField readerNumberField = new JTextField();
        JTextField nameField = new JTextField();
        JTextField departmentField = new JTextField();
        JTextField genderField = new JTextField();
        JTextField telephoneField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(5, 2));
        panel.add(new JLabel("读者编号："));
        panel.add(readerNumberField);
        panel.add(new JLabel("姓名："));
        panel.add(nameField);
        panel.add(new JLabel("部门："));
        panel.add(departmentField);
        panel.add(new JLabel("性别："));
        panel.add(genderField);
        panel.add(new JLabel("电话："));
        panel.add(telephoneField);

        int result = JOptionPane.showConfirmDialog(null, panel, "新增读者", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            try {
                String readerNumber = readerNumberField.getText();
                String name = nameField.getText();
                String department = departmentField.getText();
                String gender = genderField.getText();
                String telephone = telephoneField.getText();

                Reader reader = new Reader(readerNumber, name, department, gender, telephone);
                if (readerService.addReader(reader)) {
                    JOptionPane.showMessageDialog(null, "读者添加成功");
                    List<Reader> readers = readerService.getAllReaders();
                    updateTable(readers);
                } else {
                    JOptionPane.showMessageDialog(null, "读者添加失败，请检查输入");
                }
            } catch (IllegalArgumentException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }

    private void updateSelectedReader() {
        int selectedRow = readerTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的读者");
            return;
        }

        String readerNumber = (String) tableModel.getValueAt(selectedRow, 0);
        Reader reader = readerService.getReaderByNumber(readerNumber);

        JTextField nameField = new JTextField(reader.getName());
        JTextField departmentField = new JTextField(reader.getDepartment());
        JTextField genderField = new JTextField(reader.getGender());
        JTextField telephoneField = new JTextField(reader.getTelephone());

        JPanel panel = new JPanel(new GridLayout(4, 2));
        panel.add(new JLabel("姓名："));
        panel.add(nameField);
        panel.add(new JLabel("部门："));
        panel.add(departmentField);
        panel.add(new JLabel("性别："));
        panel.add(genderField);
        panel.add(new JLabel("电话："));
        panel.add(telephoneField);

        int result = JOptionPane.showConfirmDialog(null, panel, "修改读者信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            try {
                String name = nameField.getText();
                String department = departmentField.getText();
                String gender = genderField.getText();
                String telephone = telephoneField.getText();

                reader.setName(name);
                reader.setDepartment(department);
                reader.setGender(gender);
                reader.setTelephone(telephone);

                if (readerService.updateReader(reader)) {
                    JOptionPane.showMessageDialog(null, "读者信息修改成功");
                    List<Reader> readers = readerService.getAllReaders();
                    updateTable(readers);
                } else {
                    JOptionPane.showMessageDialog(null, "读者信息修改失败，请检查输入");
                }
            } catch (IllegalArgumentException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }

    private void deleteSelectedReader() {
        int selectedRow = readerTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的读者");
            return;
        }

        String readerNumber = (String) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(null, "确定要删除该读者吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            if (readerService.deleteReader(readerNumber)) {
                JOptionPane.showMessageDialog(null, "读者删除成功");
                List<Reader> readers = readerService.getAllReaders();
                updateTable(readers);
            } else {
                JOptionPane.showMessageDialog(null, "读者删除失败");
            }
        }
    }
}